﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessLayer.RegInfo.Models;
using DBHelper;
using BusinessLayer.RegInfo.Entities;
using System.Data.SqlClient;
using System.Data;


namespace BusinessLayer.RegInfo
{
    public class AdminUserBL
    {
        public List<AdminUserExtend> GetAll()
        {            
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            var list = adapter.GetList<AdminUserExtend>(Common.Constants.StoredProcedures.GetAllAdminUsers, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list;
        }
        public int CreateAdminUser(AdminUser user)
        {
            int result = -1;
            try
            {
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@UserName", user.UserName);
                adapter.AddParameter("@Password", user.Password);
                adapter.AddParameter("@Status", user.Status);
                adapter.AddParameter("@FirstName", user.FirstName);
                adapter.AddParameter("@LastName", user.LastName);

                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.InsertAdminUser, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;
        }
        public int UpdateAdminUser(AdminUser user)
        {
            int result = -1;
            try
            {
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@UserID", user.UserID);
                adapter.AddParameter("@UserName", user.UserName);
               
                adapter.AddParameter("@Status", user.Status);
                adapter.AddParameter("@FirstName", user.FirstName);
                adapter.AddParameter("@LastName", user.LastName);

                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.UpdateAdminUser, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;
        }

        public AdminUser GetByUserName(string username)
        {            
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@UserName", username);
            var list = adapter.GetList<AdminUser>(Common.Constants.StoredProcedures.GetAdminUserByUserName, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list.FirstOrDefault();
        }
        public AdminUser GetByUserID(int id)
        {            
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@UserID", id);
            var list = adapter.GetList<AdminUser>(Common.Constants.StoredProcedures.GetAdminUserByID, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list.FirstOrDefault();
        }
        public int DeleteAdminUserByIds(List<string> lstofids)
        {
            int result = -1;

            string ids = "";
            foreach (var id in lstofids)
            {
                ids += id + ",";
            }
            ids = String.IsNullOrEmpty(ids) ? "" : ids.Substring(0,ids.Length-1);
            if (!String.IsNullOrEmpty(ids))
            {
                try
                {
                    var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                    result = adapter.ExecuteNonQuery(String.Format("DELETE FROM AdminUsers WHERE UserID IN ({0});", ids), CommandType.Text, CustomConnectionState.CloseOnExit);
                    return result;
                }
                catch (Exception ex)
                {

                }
            }
            return result;
        }
        //GetAdminUserByUserGroupID
        public List<AdminUser> GetAdminUsersByUserGroupID(int usergroupid)
        {            
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@UserGroupID", usergroupid);
            var list = adapter.GetList<AdminUser>(Common.Constants.StoredProcedures.GetAdminUserByUserGroupID, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list;
        }
        public bool ValidateUser(string username, string password)
        {
            bool result = false;            
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@UserName", username);
            adapter.AddParameter("@Password", password);
            var list = adapter.GetList<AdminUser>(Common.Constants.StoredProcedures.GetAdminUserByUserNameAndPassword, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            if(list==null)
            {
                return false;
            }
            return list.Count != 0;
        }
        public bool ChangePassword(int userid,string oldPassword, string newPassword)
        {
            bool result = false;
            try
            {                
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@UserID", userid);
                adapter.AddParameter("@Password", oldPassword);
                adapter.AddParameter("@NewPassword", newPassword);

                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.ChangePasswordAdminUser, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit) > 0;
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;
        }

        public bool ResetPassword(int userid, string newPassword)
        {
            bool result = false;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.ResetPasswordAdminUser) { CommandType = CommandType.StoredProcedure };
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@UserID", userid);
                adapter.AddParameter("@NewPassword", newPassword);


                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.ResetPasswordAdminUser, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit) > 0;
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;
        }

        public bool ChangePasswordByUserName(string username, string oldPassword, string newPassword)
        {
            bool result = false;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.ChangePasswordAdminUserByUserName) { CommandType = CommandType.StoredProcedure };
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@UserName", username);

                adapter.AddParameter("@Password", oldPassword);
                adapter.AddParameter("@NewPassword", newPassword);


                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.ChangePasswordAdminUserByUserName, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit) > 0;
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;
        }

        public bool ResetPasswordByUserName(string username, string newPassword)
        {
            bool result = false;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.ResetPasswordAdminUserByUserName) { CommandType = CommandType.StoredProcedure };
                var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
                adapter.AddParameter("@UserName", username);
                adapter.AddParameter("@NewPassword", newPassword);

                result = adapter.ExecuteNonQuery(Common.Constants.StoredProcedures.ResetPasswordAdminUserByUserName, CommandType.StoredProcedure, CustomConnectionState.CloseOnExit) > 0;
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;
        }
    }
}
